package com.matt.controller.web.courseV2;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.VendorCourseRecord;
import com.matt.domain.courseV2.po.CourseDetailsPO;
import com.matt.domain.courseV2.po.VendorCourseRecordPO;
import com.matt.domain.courseV2.vo.VendorRecordVO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.courseV2.VendorRecordService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/api/vendor_record")
@Slf4j
@Api(tags = "教学 - 学习记录管理")
public class VendorRecordController {

    @Autowired
    private VendorRecordService vendorRecordService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "添加 - 学生学习记录")
    @PostMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = CourseDetailsPO.class),
    })
    public StatusResponse addVendorRecord(@RequestHeader(TICKET) String ticket,VendorRecordVO vendorRecordVO) {
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        vendorRecordService.add(staffDTO,vendorRecordVO);
        return StatusResponse.success();
    }


    @ApiOperation(value = "动态发布 - 列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = CourseDetailsPO.class),
    })
    public StatusResponse list(@RequestHeader(TICKET) String ticket, PagerQuery pagerQuery ) {
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        Page<VendorCourseRecordPO>   list = vendorRecordService.list(staffDTO.getAreaId() , pagerQuery);
        return StatusResponse.success(list);
    }

}
